//
//  JYCaculateTool.swift
//  JYCalculate
//
//  Created by 何治军 on 2018/10/29.
//  Copyright © 2018 何治军. All rights reserved.
//

import UIKit

class JYCaculateTool: NSObject {

}
///四舍五入 - 获取保留 digit 位小数的字符串，若digit小于0，则保留 SZ_Digit 位小数
//SZ_INLINE NSString * NTSWithDigit(NSDecimalNumber* number, NSUInteger digit) {
//
//    digit = digit < 0 ? SZ_Digit : digit;
//
//    NSString *result = [number stringValue];
//    NSDecimalNumberHandler *behavior = [[NSDecimalNumberHandler alloc]initWithRoundingMode:NSRoundPlain scale:digit raiseOnExactness:false raiseOnOverflow:false raiseOnUnderflow:false raiseOnDivideByZero:true];
//
//    result = [[number decimalNumberByRoundingAccordingToBehavior:behavior] stringValue];
//    
//    if (!result) return @"";
//    return result;
//}
////只舍不入 - 获取保留 digit 位小数的字符串，若digit小于0，则保留 SZ_Digit 位小数
//SZ_INLINE NSString * DownWithDigit(NSDecimalNumber* number, NSUInteger digit) {
//
//    digit = digit < 0 ? SZ_Digit : digit;
//
//    NSString *result = [number stringValue];
//    NSDecimalNumberHandler *behavior = [[NSDecimalNumberHandler alloc]initWithRoundingMode:NSRoundDown scale:digit raiseOnExactness:false raiseOnOverflow:false raiseOnUnderflow:false raiseOnDivideByZero:true];
//
//    result = [[number decimalNumberByRoundingAccordingToBehavior:behavior] stringValue];
//
//    if (!result) return @"";
//    return result;
//}
//
////加法
//SZOVERLOADABLE SZ_INLINE NSDecimalNumber* AdditionM(NSString *a, NSString *b) {
//    NSDecimalNumber *ad = [NSDecimalNumber decimalNumberWithString:a];
//    NSDecimalNumber *bd = [NSDecimalNumber decimalNumberWithString:b];
//    return [ad decimalNumberByAdding:bd];
//}
////减法
//SZOVERLOADABLE SZ_INLINE NSDecimalNumber* SubtractionM(NSString *a, NSString *b) {
//    NSDecimalNumber *ad = [NSDecimalNumber decimalNumberWithString:a];
//    NSDecimalNumber *bd = [NSDecimalNumber decimalNumberWithString:b];
//    return [ad decimalNumberBySubtracting:bd];
//}
////乘法
//SZOVERLOADABLE SZ_INLINE NSDecimalNumber* MultiplicationM(NSString *a, NSString *b) {
//    NSDecimalNumber *ad = [NSDecimalNumber decimalNumberWithString:a];
//    NSDecimalNumber *bd = [NSDecimalNumber decimalNumberWithString:b];
//    return [ad decimalNumberByMultiplyingBy:bd];
//}
////除法
//SZOVERLOADABLE SZ_INLINE NSDecimalNumber* DivisionM(NSString *a, NSString *b) {
//    NSDecimalNumber *ad = [NSDecimalNumber decimalNumberWithString:a];
//    NSDecimalNumber *bd = [NSDecimalNumber decimalNumberWithString:b];
//    return [ad decimalNumberByDividingBy:bd];
//}

let SZ_Digit:Int16 = 2
///四舍五入 - 获取保留 digit 位小数的字符串，若digit小于0，则保留 SZ_Digit 位小数
func NTSWithDigit(_ number: NSDecimalNumber , digit:Int16) -> String{
    let num = digit < 0 ? SZ_Digit : digit
    var result = number.stringValue
    let behavior = NSDecimalNumberHandler.init(roundingMode: .plain, scale: num, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: true)
    result = number.rounding(accordingToBehavior: behavior).stringValue
    return result;
}
///只舍不入 - 获取保留 digit 位小数的字符串，若digit小于0，则保留 SZ_Digit 位小数
func DownWithDigit(_ number: NSDecimalNumber , digit:inout Int16) -> String{
    digit = digit < 0 ? SZ_Digit : digit
    var result = number.stringValue
    let behavior = NSDecimalNumberHandler.init(roundingMode: .down, scale: digit, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: true)
    result = number.rounding(accordingToBehavior: behavior).stringValue
    return result;
}
//加法
func AdditionM(_ a:String, b:String) ->NSDecimalNumber {
    let ad = NSDecimalNumber.init(string: a)
    let bd = NSDecimalNumber.init(string: b)
    return ad.adding(bd)
}
//减法
func SubtractionM(_ a:String, b:String) ->NSDecimalNumber{
    let ad = NSDecimalNumber.init(string: a)
    let bd = NSDecimalNumber.init(string: b)
    return ad.subtracting(bd)
}
//乘法
func MultiplicationM(_ a:String, b:String) ->NSDecimalNumber{
    let ad = NSDecimalNumber.init(string: a)
    let bd = NSDecimalNumber.init(string: b)
    return ad.multiplying(by: bd)
}
//除法
func DivisionM(_ a:String, b:String) ->NSDecimalNumber{
    let ad = NSDecimalNumber.init(string: a)
    let bd = NSDecimalNumber.init(string: b)
    return ad.dividing(by: bd)
}
